from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI 

class kmeans(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)

        
        # Python variables
        self.data = {}
        self.RFunctionParam_object = ''
        
        #Unique R variable
        self.setRvariableNames(['cascadeKM'])
        
        #Inputs
        self.inputs.addInput('id0', _('R Dataframe Object'), signals.base.RDataFrame, self.processobject)
		
        #Ouputs
        self.outputs.addOutput('id0', 'K-means results', signals.base.RList)

        
        #GUI
        self.RFunctionParam_koptions = redRGUI.base.groupBox(self.controlArea, label='K-means Options', orientation='horizontal')
        self.RFunctionParam_infGR = redRGUI.base.spinBox(self.RFunctionParam_koptions, label='Smallest number of groups', min = 2, max=100, value = 2 )
        self.RFunctionParam_supGR = redRGUI.base.spinBox(self.RFunctionParam_koptions, label='Largest number of groups', min = 2, max=100, value = 10 )
        self.RFunctionParam_iter = redRGUI.base.spinBox(self.RFunctionParam_koptions, label='Number of iterations', min = 2, max=1000, value = 100 )
        self.RFunctionParam_criterion = redRGUI.base.comboBox(self.RFunctionParam_koptions,  label = "Criterion: ",
        items = [('calinski','Calinski'),('ssi','SSI')],editable=False) #, ('cindex','C-Index'),('db','DB'),('hartigan','Hartigan'),('ratkowsky','Ratkowsky'),        ('scott','Scott'),('marriot','Marriot'),('ball','Ball'),('trcovw','Trcovw'),('tracew','Trace-w'),('friedman','Friedman'),('rubin','Rubin'),        ('likelihood','Likelihood'),('all','All')
        
        self.RFunctionParam_poptions = redRGUI.base.groupBox(self.controlArea, label='Plot Options', orientation='horizontal')
        self.RFunctionParam_sortg = redRGUI.base.radioButtons(self.RFunctionParam_poptions,  label = "Sort", 
        buttons = [('TRUE','Yes'),('FALSE','No')],
        setChecked='TRUE',orientation='horizontal')
        
        self.plotArea = redRGUI.plotting.redRPlot(self.controlArea, label = "Plot", displayLabel = False)
        self.plotArea.resizeCheck.uncheckAll()
        self.plotArea.topArea.hide()
        
        #Commit
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        
    def processobject(self, data):
        if not self.require_librarys(["vegan"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''
    def commitFunction(self):
        
        if unicode(self.RFunctionParam_object) == '':
            self.status.setText('No data.')
            return

        injection = []
        injection.append('inf.gr='+unicode(self.RFunctionParam_infGR.value()))
        injection.append('sup.gr='+unicode(self.RFunctionParam_supGR.value()))
        injection.append('iter='+unicode(self.RFunctionParam_iter.value()))
        injection.append('criterion="'+unicode(self.RFunctionParam_criterion.currentId())+'"')
        inj = ','.join(injection)
        
        self.R(self.Rvariables['cascadeKM']+'<-cascadeKM(data='+unicode(self.RFunctionParam_object)+', '+inj+')')

        self.plotArea.plotMultiple(query = self.Rvariables['cascadeKM']+', sortg='+unicode(self.RFunctionParam_sortg.getCheckedId()), function = 'plot')
        
        self.rSend('id0', signals.base.RList(self, data = self.Rvariables['cascadeKM'], checkVal = False))